﻿#region Copyright
/* © 2008 George Tsiokos, licensed under the Microsoft Reciprocal License (Ms-RL) */
#endregion
using System;
using System.Runtime.InteropServices;
using System.Text;

namespace Comet.Collections.Generic {
    /// <summary>Experimental</summary>
    [Serializable, StructLayout (LayoutKind.Sequential)]
    public struct TemporalIndex : IComparable<TemporalIndex>, IEquatable<TemporalIndex>, IComparable {

        #region Fields (2)

        private readonly long _timestamp;
        private readonly int _index;

        #endregion Fields

        #region Constructors (1)

        internal TemporalIndex (long Timestamp, int index) {
            _timestamp = Timestamp;
            _index = index;
        }

        #endregion Constructors

        #region Properties (2)

        public int Index {
            get {
                return _index;
            }
        }

        public long Timestamp {
            get {
                return _timestamp;
            }
        }

        #endregion Properties

        #region Methods (5)

        public int CompareTo (object obj) {
            if (obj == null)
                return 1;
            else {
                if (!(obj is TemporalIndex))
                    throw new ArgumentException ("not a TemporalIndex", "obj");
                else
                    return CompareTo ((TemporalIndex)obj);
            }
        }

        public int CompareTo (TemporalIndex other) {
            int timestampResult = _timestamp.CompareTo (other._timestamp);
            if (timestampResult != 0)
                return timestampResult;
            else
                return _index.CompareTo (other._index);
        }

        public bool Equals (TemporalIndex other) {
            return base.Equals (other);
        }

        public override string ToString () {
            StringBuilder stringBuilder = new StringBuilder ();
            stringBuilder.Append ('[');
            stringBuilder.Append (_timestamp);
            stringBuilder.Append (", ");
            stringBuilder.Append (_index);
            stringBuilder.Append (']');
            return stringBuilder.ToString ();
        }

        #endregion Methods
    }
}